import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from 'views/home/home'
// import welcome from 'views/home/childcoms/welcome'
// !路由懒加载 es6之后就不用导入插件了  可以直接使用 不知道可不可以 像我这样 指定文件夹就不知道了
const welcome = () => import(/* webpackChunkName: "group-one" */ 'views/home/childcoms/welcome')
const users = () => import(/* webpackChunkName: "group-one" */ 'views/home/childcoms/users/users')
const rights = () => import(/* webpackChunkName: "group-two" */ 'views/home/childcoms/power/rights')
const roles = () => import(/* webpackChunkName: "group-two" */ 'views/home/childcoms/power/roles')
const cate = () => import(/* webpackChunkName: "group-three" */ 'views/home/childcoms/goods/cate')
const params = () => import(/* webpackChunkName: "group-three" */ 'views/home/childcoms/goods/params')
const goods = () => import(/* webpackChunkName: "group-five" */ 'views/home/childcoms/goods/goods')
const goodsAdd = () => import(/* webpackChunkName: "group-five" */ 'views/home/childcoms/goods/goods_add')
const orders = () => import(/* webpackChunkName: "group-five" */ 'views/home/childcoms/orders/orders')
const reports = () => import(/* webpackChunkName: "group-five" */ 'views/home/childcoms/reports/reports')
const Login = () => import(/* webpackChunkName: "group-five" */ 'views/home/childcoms/reports/reports')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: welcome },
      { path: '/users', component: users },
      { path: '/rights', component: rights },
      { path: '/roles', component: roles },
      { path: '/categories', component: cate },
      { path: '/params', component: params },
      { path: '/goods', component: goods },
      { path: '/orders', component: orders },
      { path: '/reports', component: reports },
      { path: '/goodadd', component: goodsAdd }
    ]
  }
]

const router = new VueRouter({
  routes
})
// !这里好像就是设置权限一样的东西
// 这个玩意就是导航守卫  to代表去的位置   from表示从哪里跳转到这里  next表示放行
router.beforeEach((to, from, next) => {
  // 1.判断我的去的是不是login 是的话直接放行
  if (to.path === '/login') return next()
  // 2.判断一下我的浏览器中有没有token这个乱码
  const tokenstr = window.sessionStorage.getItem('token')
  // 3.如果没有的话就会用next跳转到login
  if (!tokenstr) return next('/login')
  // 4.如果又就会放行
  next()
})

export default router
